package com.younion.action.consumer.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IFavourableEntityService;
import com.younion.business.IFavourableService;
import com.younion.common.constants.SysConstants;
import com.younion.model.FavListResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.API_FavourableEntityVO;
import com.younion.vo.FavourableVO;

public class FavourableListGetConsumer extends AbstractRequestConsumer {
	private IFavourableService favourableService;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getFavourableList(request, response);
	}

	public String getFavourableList(HttpServletRequest request,
			HttpServletResponse response) {
		String user_token = request.getParameter("user_token");
		String member_card_id = request.getParameter("member_card_id");
		int int_page = ApiConstant.API_DEFAULT_PAGE;
		List<FavourableVO> favourableList = new ArrayList<FavourableVO>();
		List<FavListResult> FavsListResultList = new ArrayList<FavListResult>();
		Map paramMap = new HashMap();
		ResultObject resultObject = new ResultObject();

		try {
			paramMap.put("usertoken", user_token);
			if (member_card_id != null
					&& HttpRequestUtils.checkIntegerParam("member_card_id",
							member_card_id, response)) {
				// 根据member_card_id查询卡所属商家下的优惠券实例
				paramMap.put("member_customer_id", member_card_id);
			}
			//状态为可使用的
			paramMap.put("state", 0);
			if (user_token != null && !"".equals(user_token)) {
				favourableList = (List<FavourableVO>) favourableService
						.getByMap(paramMap);
			}
			processFavData(favourableList, FavsListResultList);
			resultObject.setResult(FavsListResultList);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	private void processFavData(List<FavourableVO> favourableList,
			List<FavListResult> FavsListResultList) {
		String img_url_pre = ConfigHelper.fastGetConfig("interface",
				"img_url_pre", "http://localhost:8080/younonmember/");
		try {
			if (favourableList != null && favourableList.size() > 0) {

				for (FavourableVO favourable : favourableList) {
					FavListResult favsListResult = new FavListResult();
					API_FavourableEntityVO api_favourableEntityVO = favourable
							.getApi_favourableEntity();
					favsListResult
							.setCat_id(api_favourableEntityVO.getCat_id());
					favsListResult.setFav_id(favourable.getId().toString());
					favsListResult
							.setFav_name(api_favourableEntityVO.getName());
					SimpleDateFormat sf = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					favsListResult.setNonactivatedPic(img_url_pre
							+ SysConstants.FavourableEntity_IMG_URL + "/"
							+ api_favourableEntityVO.getId() + "/"
							+ api_favourableEntityVO.getNonactivatedPic());
					favsListResult.setActivatedPic(img_url_pre
							+ SysConstants.FavourableEntity_IMG_URL + "/"
							+ api_favourableEntityVO.getId() + "/"
							+ api_favourableEntityVO.getActivatedPic());
					favsListResult.setMoney(api_favourableEntityVO.getValue());
					favsListResult.setCode(favourable.getCode());
					favsListResult.setState(favourable.getState().toString());
					favsListResult.setApply_date_from((api_favourableEntityVO
							.getApplyBegDate() == null) ? "" : sf
							.format(api_favourableEntityVO.getApplyBegDate()));
					favsListResult.setApply_date_to((api_favourableEntityVO
							.getApplyEndDate() == null) ? "" : sf
							.format(api_favourableEntityVO.getApplyEndDate()));
					favsListResult.setUse_date_from((api_favourableEntityVO
							.getUseBegDate() == null) ? "" : sf
							.format(api_favourableEntityVO.getUseBegDate()));
					favsListResult.setUse_date_to((api_favourableEntityVO
							.getUseEndDate() == null) ? "" : sf
							.format(api_favourableEntityVO.getUseEndDate()));
					favsListResult.setSimple_description(api_favourableEntityVO
							.getSimpleDescription());
					if (api_favourableEntityVO.getParticipate_num() != null) {
						favsListResult
								.setParticipate_num(api_favourableEntityVO
										.getParticipate_num().toString());
					} else {
						favsListResult.setParticipate_num("0");
					}
					favsListResult.setFav_ent_id(api_favourableEntityVO.getId()
							.toString());
					favsListResult.setGainManner(favourable.getGainManner().toString());
					favsListResult.setIsAuthenByMoblie(api_favourableEntityVO.getIsAuthenByMoblie());
					FavsListResultList.add(favsListResult);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void setFavourableService(IFavourableService favourableService) {
		this.favourableService = favourableService;
	}

}
